<?php
/**
 * 订单管理模型
 * User: John
 * Date: 17-1-3
 * Time: 上午11:18
 */

namespace app\wxbackend\model;

use think\Model;

class BillManager extends Model
{
    private $payBillTable     = 'z_pay_bill';
    private $leaseExtTable    = 'z_contract_order_ext';
    private $payComment       = 'z_pay_bill_comment';
    private $trustBillTable   = 'z_trust_bill';
    private $memberTable      = 'z_member';
    private $settleRuleTable  = 'z_settle_rule';
    private $alipayTable      = 'z_alipay';
    private $manualCollection = 'z_manual_collection';
    private $_insertLimit     = 1000;

    public function __construct() {
        parent::__construct();
    }

    /**
     * 查看租户账单明细
     * @date   2017-02-06
     * @author wuhao
     *
     * @param  string  $orderNum    合同编号
     * @param  string  $beginCycle  周期开始
     * @param  string  $endCycle    周期结束
     *
     * @return array|bool
     */
    public function leaseBillDetail($orderNum, $beginCycle, $endCycle)
    {
        if (!$orderNum  || !$beginCycle || !$endCycle) {
            return false;
        }

        $where = ['ordernum' => $orderNum, 'begin_cycle' => $beginCycle, 'end_cycle' => $endCycle, 'is_delete' => 0];
        $field = 'id, owner_id, free_money, money, type_money, tenant_id, bill_name, begin_cycle, end_cycle';
        return $this->setTable($this->payBillTable)->field($field)->where($where)->select();
    }

    /**
     * 获取租赁账单信息
     * @date   2017-01-17
     * @author wuhao
     *
     * @param  array   $where     条件
     * @param  string  $field     获取字段
     *
     * @return array
     */
    public function getPayBillInfo($where, $field = '*')
    {
        return $this->setTable($this->payBillTable)->field($field)->where($where)->select();
    }

    /**
     * 更新租赁账单信息
     * @date   2017-01-20
     * @author Lanlc
     *
     * @param  array  $where  更新条件
     * @param  array  $data   更新数据
     *
     * @return
     */
    public function updatePayBill($where, $data)
    {
        return $this->setTable($this->payBillTable)->where($where)->update($data);
    }

    /**
     * 获取租赁账单支付状态
     * @date   2017-02-08
     * @author wuhao
     *
     * @param  string  $orderNum   订单号
     * @param  int     $beginCycle 账单开始时间
     * @param  int     $endCycle   账单结束时间
     * @return array
     */
    public function payBillStatus($orderNum, $beginCycle, $endCycle)
    {
        $where = [
            'ordernum'    => $orderNum,
            'begin_cycle' => $beginCycle,
            'end_cycle'   => $endCycle,
            'is_delete'   => 0,
        ];
        $field = 'status';
        return $this->setTable($this->payBillTable)->field($field)->where($where)->find();
    }

    /**
     * 获取租赁账单
     * @date   2017-03-23
     * @author wuhao
     *
     * @param  array    $where   条件
     * @param  string   $field   字段
     * @param  int      $page    页数
     * @param  int      $pageSize 每页个数
     * @return array
     */
    public function getPayBill($where, $field = '*', $page = 1, $pageSize = 10)
    {
        return $this->setTable($this->payBillTable)->field($field)->where($where)->page($page, $pageSize)->select();
    }

    /**
     * 获取租赁账单
     * @date   2017-03-23
     * @author wuhao
     *
     * @param  array    $where   条件
     * @param  string   $field   字段
     * @return array
     */
    public function getPayBillList($where, $field = '*')
    {
        return $this->setTable($this->payBillTable)->field($field)->where($where)->select();
    }

    /**
     * 获取租户租赁账单
     * @date   2017-8-30
     * @author wuhao
     *
     * @param  int     $memberId     用户id
     * @param  string  $endMonth     查询结束时间
     * @param  int     $status       应付订单状态 0=未支付 1=已支付
     * @param  int     $page         页数
     * @param  int     $pageSize     每页个数
     *
     * @return array|bool
     */
    public function leaseBill($memberId, $endMonth = '', $status = 0, $page = 1, $pageSize = 15)
    {
        if (!$memberId) {
            return false;
        }
        $where = [];

        $where['p.tenant_id']  = $memberId;
        $where['p.begin_cycle']  = ['elt', $endMonth];

        if (is_numeric($status)) {
            $where['p.status']     = $status;
        }
        $where['p.is_delete']   = 0;
        $where['e.status']      = 1;

        $field = 'p.ordernum, p.tenant_id, p.status, p.begin_cycle, p.end_cycle, sum(p.money) total_money';
        $group = 'p.tenant_id, p.ordernum, p.begin_cycle, p.end_cycle, p.status';
        $res   = $this->setTable($this->payBillTable.' p')->join( $this->leaseExtTable .' e' ,'e.ordernum=p.ordernum','LEFT')->field($field)
            ->where($where)->group($group)->order('p.begin_cycle desc')->page($page, $pageSize)->select();
        return $res;
    }


    /**
     * 获取租赁账单总数
     * @date   2017-8-30
     * @author wuhao
     *
     * @param  int     $memberId     用户id
     * @param  string  $endMonth     查询结束时间
     * @param  int     $status       应付订单状态 0=未支付 1=已支付 为空的话不限制
     *
     * @return array|bool
     */
    public function countLeaseBill($memberId, $endMonth = '', $status = 0)
    {
        if (!$memberId) {
            return false;
        }
        $where = [];
        $content = 'distinct p.tenant_id, p.ordernum, p.begin_cycle, p.end_cycle, p.status';
        $where['p.tenant_id']  = $memberId;
        $where['p.begin_cycle']  = ['elt', $endMonth];

        if (is_numeric($status)) {
            $where['p.status']     = $status;
        }
        $where['p.is_delete']   = 0;
        $where['e.status']      = 1;

        $res =  $this->setTable($this->payBillTable.' p')
            ->join( $this->leaseExtTable .' e' ,'e.ordernum=p.ordernum','LEFT')
            ->where($where)
            ->count($content);
        return $res;
    }

    /**
     * 租赁账单 --获取收款人或付款人的某个账单的详情
     * @date   2017-05-18
     * @author wuhao
     *
     * @param  int     $ownerId     收款人或付款人id
     * @param  int     $dtype       用户类型
     * @param  string  $ordernum    合同编号
     * @param  int     $beginCycle  账单开始周期
     * @param  int     $endCycle    账单结束周期
     * @param  string  $status      结算状态 0=没结算 1=已结算 为空不限制
     * @param  string  $field       要获取的字段
     * @return bool|array
     */
    public function getBillDetailByOwner($ownerId, $dtype, $ordernum, $beginCycle, $endCycle, $status = '', $field = '*')
    {
        if (!$ownerId || !$ordernum || !$beginCycle || !$endCycle || !in_array($dtype, [1, 2, 3, 4, 5, 6])) {
            return false;
        }
        $where['ordernum'] = $ordernum;
        if ($dtype != 2) {
            $where['owner_id'] = $ownerId;
        } else {
            $where['tenant_id'] = $ownerId;
        }

        $where['begin_cycle'] = $beginCycle;
        $where['end_cycle'] = $endCycle;
        if (in_array($status, [0 ,1])) {
            $where['status'] = $status;
        }
        $where['is_delete'] = 0;
        return $this->setTable($this->payBillTable)->field($field)->where($where)->select();
    }

}
